package cn.com.nes.site.service.project.impl;

import cn.com.nes.common.exception.MissBusinessParameterException;
import cn.com.nes.mybatis.agent.project.entity.ProjectCustomerRelationship;
import cn.com.nes.mybatis.agent.project.mapper.ProjectCustomerRelationshipMapper;
import cn.com.nes.site.entity.bo.project.ProjectCustomerRelationshipPageBO;
import cn.com.nes.site.entity.em.project.ProjectRelationTypeEnum;
import cn.com.nes.site.service.project.ProjectCustomerRelationshipService;
import cn.com.nes.site.service.project.dto.ProjectCustomerRelationCountDTO;
import cn.com.nes.site.service.project.dto.ProjectRelationshipDTO;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author yishuai
 */
@Slf4j
@Service
public class ProjectCustomerRelationshipServiceImpl extends ServiceImpl<ProjectCustomerRelationshipMapper, ProjectCustomerRelationship> implements ProjectCustomerRelationshipService {

    /**
     * 根据项目ID，查询所有客户关系列表
     * @param projectId 项目ID
     * @return
     */
    @Override
    public List<ProjectCustomerRelationship> getAllRelationship(String projectId) throws Exception {
        QueryWrapper<ProjectCustomerRelationship> entityWrapper = new QueryWrapper<>();
        entityWrapper.eq("deleted", false);
        entityWrapper.eq("project_uuid", projectId);
        List<ProjectCustomerRelationship> projectCustomerRelationships = this.baseMapper.selectList(entityWrapper);
        return projectCustomerRelationships;
    }

    @Override
    public List<ProjectCustomerRelationship> getRelationshipsExcludeMain(String projectId) throws Exception {
        QueryWrapper<ProjectCustomerRelationship> entityWrapper = new QueryWrapper<>();
        entityWrapper.eq("deleted", false);
        entityWrapper.eq("main_flag", false);
        entityWrapper.eq("project_uuid", projectId);
        List<ProjectCustomerRelationship> projectCustomerRelationships = this.baseMapper.selectList(entityWrapper);
        return projectCustomerRelationships;
    }

    @Override
    public List<ProjectCustomerRelationship> getRelationshipsExcludeMain(String projectId,
            String customerId) throws Exception {
        QueryWrapper<ProjectCustomerRelationship> entityWrapper = new QueryWrapper<>();
        entityWrapper.eq("deleted", false);
        entityWrapper.eq("main_flag", false);
        entityWrapper.eq("project_uuid", projectId);
        entityWrapper.eq("customer_id", customerId);
        List<ProjectCustomerRelationship> projectCustomerRelationships = this.baseMapper.selectList(entityWrapper);
        return projectCustomerRelationships;
    }

    @Override
    public List<ProjectCustomerRelationship> getRelationshipsForImport(String projectId) throws Exception {
        QueryWrapper<ProjectCustomerRelationship> entityWrapper = new QueryWrapper<>();
        entityWrapper.eq("deleted", false);
        entityWrapper.eq("main_flag", false);
        entityWrapper.eq("data_type", 0);
        entityWrapper.eq("project_uuid", projectId);
        List<ProjectCustomerRelationship> projectCustomerRelationships = this.baseMapper.selectList(entityWrapper);
        return projectCustomerRelationships;
    }


    /**
     * 查询主客户关系
     * @param projectUuid
     * @param opportunityUuid
     * @return
     */
    @Override
    public ProjectCustomerRelationship getRelationshipsForMain(String projectUuid,
            String opportunityUuid) throws Exception {
        QueryWrapper<ProjectCustomerRelationship> wrapper = new QueryWrapper<ProjectCustomerRelationship>().eq("deleted", false)
                .eq("main_flag", ProjectRelationTypeEnum.MAIN.getKey())
                .eq(StrUtil.isNotEmpty(projectUuid), "project_uuid", projectUuid)
                .eq("main_business_id", opportunityUuid);
        return this.getOne(wrapper);
    }

    @Override
    public ProjectCustomerRelationship getMainRelationshipByOpportunityUuid(String opportunityUuid) throws Exception {
        QueryWrapper<ProjectCustomerRelationship> wrapper = new QueryWrapper<ProjectCustomerRelationship>().eq("deleted", false)
                .eq("main_flag", ProjectRelationTypeEnum.MAIN.getKey())
                .eq("main_business_id", opportunityUuid);
        return this.getOne(wrapper);
    }

    /**
     * 查询主客户关系
     * @param projectId
     * @return
     */
    @Override
    public List<ProjectCustomerRelationship> getRelationshipsForMain(String projectId) throws Exception {
        QueryWrapper<ProjectCustomerRelationship> entityWrapper = new QueryWrapper<>();
        entityWrapper.eq("deleted", false);
        entityWrapper.eq("main_flag", true);
        entityWrapper.eq("project_uuid", projectId);
        return this.baseMapper.selectList(entityWrapper);
    }

    /**
     * 根据项目ID，查询客户关系不包含主客户的列表
     * @param bo
     * @return
     */
    @Override
    public Page<ProjectRelationshipDTO> getRelationshipsExcludeMain(
            ProjectCustomerRelationshipPageBO bo) throws Exception {
        Page<ProjectRelationshipDTO> page = new Page<>(bo.getPageid(), bo.getPagesize()); // 准备分页参数
        List<ProjectRelationshipDTO> projectCustomerRelationships = this.baseMapper.getRelationshipsExcludeMain(page, bo);
        return page.setRecords(projectCustomerRelationships);
    }

    @Override
    public ProjectCustomerRelationship hasMainCustomer(String projectUuid) throws Exception {
        ProjectCustomerRelationship projectCustomerRelationship = new ProjectCustomerRelationship();
        projectCustomerRelationship.setProjectUuid(projectUuid);
        projectCustomerRelationship.setMainFlag(1);
        projectCustomerRelationship.setDeleted(0);
        ProjectCustomerRelationship mainRelationship = this.baseMapper.selectOne(new QueryWrapper<>(projectCustomerRelationship));
        log.info("ProjectCustomerRelationship {}", mainRelationship);
        return mainRelationship;
    }

    /**
     * 删除导入用户
     * @param projectUuid   项目ID
     * @param operateUserId 操作人
     * @param operateDate   操作时间
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteImportCustomer(String projectUuid, Long operateUserId, Date operateDate) throws Exception {
        this.baseMapper.deleteImportCustomer(projectUuid, operateUserId, operateDate);
    }

    @Override
    public List<String> getCustomerIdListByXzqhdm(String projectUuid, String xzqhdm) {
        return this.baseMapper.getCustomerIdListByXzqhdm(projectUuid, xzqhdm);
    }

    @Override
    public void deleteImportCustomerByCustomerId(List<String> customerIdList, Long operateUserId, Date operateDate) {
        if (CollUtil.isEmpty(customerIdList)) {
            throw new MissBusinessParameterException("请传入要删除的客户ID");
        }
        this.baseMapper.deleteImportCustomerByCustomerId(customerIdList, operateUserId, operateDate);
    }

    @Override
    public List<ProjectCustomerRelationCountDTO> getCustomerCountOfProject(List<String> projectUuids) throws Exception {
        return this.baseMapper.getCustomerCountOfProjectExcludeMain(projectUuids);
    }

    @Override
    public void updateRelationshipsByMainCustomerId(String projectUuid, String mainOpportunityId, Long operateUserId,
            Date operateDate) {
        if (StringUtils.isEmpty(projectUuid)) {
            throw new MissBusinessParameterException("请传入项目ID");
        }
        if (StringUtils.isEmpty(mainOpportunityId)) {
            throw new MissBusinessParameterException("请传入主商机id");
        }
        this.baseMapper.updateRelationshipsByMainCustomerId(projectUuid, mainOpportunityId, operateUserId, operateDate);
    }

    @Override
    public void deleteOpportunityId(List<String> opportunityIdList, String opportunityId, Long operateUserId,
            Date operateDate) {
        if (CollUtil.isEmpty(opportunityIdList)) {
            throw new MissBusinessParameterException("请传入要删除的商机ID");
        }
        this.baseMapper.deleteOpportunityId(opportunityIdList, opportunityId, operateUserId, operateDate);
    }

    @Override
    public List<ProjectCustomerRelationship> getBeRelatedOfRelationships(String mainOpportunityUuid) throws Exception {
        QueryWrapper<ProjectCustomerRelationship> entityWrapper = new QueryWrapper<>();
        entityWrapper.eq("deleted", false);
        entityWrapper.eq("main_flag", false);
        entityWrapper.eq("main_business_id", mainOpportunityUuid);
        return this.baseMapper.selectList(entityWrapper);
    }

    @Override
    public ProjectCustomerRelationship getOtherRelationship(String customerUuid,
            String mainOpportunityUuid) throws Exception {
        QueryWrapper<ProjectCustomerRelationship> wrapper = new QueryWrapper<ProjectCustomerRelationship>().eq("deleted", false)
                .eq("main_flag", ProjectRelationTypeEnum.OTHER.getKey())
                .eq("customer_id", customerUuid)
                .eq("main_business_id", mainOpportunityUuid);
        return this.getOne(wrapper);
    }

    @Override
    public ProjectCustomerRelationship queryProjectCustomerRelationship(String customerId, String projectUuid) {
        ProjectCustomerRelationship projectCustomerRelationship = new ProjectCustomerRelationship();

        QueryWrapper<ProjectCustomerRelationship> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", customerId);
        queryWrapper.eq("project_uuid", projectUuid);
        queryWrapper.eq("deleted", false);
        List<ProjectCustomerRelationship> resultList = this.baseMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(resultList)) {
            BeanUtils.copyProperties(resultList.get(0), projectCustomerRelationship);
        }
        return projectCustomerRelationship;
    }

}




